import 'dart:convert';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:test_flutter/cache/UserDataCache.dart';
import 'package:test_flutter/constant/resources.dart';
import 'package:test_flutter/model/response/user_model.dart';
import 'package:test_flutter/pages/HomePage.dart';
import 'dart:async';
import 'package:test_flutter/pages/LoginPage.dart';
import 'package:test_flutter/utils/common_utils/CustomLocalizations.dart';
import 'package:test_flutter/utils/factory/Fit.dart';
import 'package:test_flutter/utils/common_utils/CustomLocalizations.dart';

class SplashPage extends StatefulWidget {
  SplashPage({Key key}) : super(key: key);
  @override
  _SplashPage createState() => new _SplashPage();
}

class _SplashPage extends State<SplashPage> {
  bool isStartHomePage = false;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    CustomLocalizations.init(context);
    Fit.init(context, 1080, 1920);//适配方案初始化
    return GestureDetector(
        onTap: goToHomePage, //设置页面点击事件
        child: new Container(
            child: Stack(
          children: [
            Image.asset(
              'images/spbg.gif',
              width: Fit.scaleX(1080),
              height: Fit.scaleY(1920),
              fit: BoxFit.fill,
            ),
            BackdropFilter(
              filter: new ImageFilter.blur(sigmaX: 5, sigmaY: 5),
              child: new Container(
                color: Colors.white.withOpacity(0.1),
                width: Fit.scaleX(1080),
                height: Fit.scaleY(1920),
              ),
            ),
            Center(
              child: Text(CustomLocalizations.instance
                  .getString(StringIds.titleWashing), style: TextStyle(color: Colors.white,inherit: false,fontSize: Fit.scaleTextSize(100),fontStyle: FontStyle.italic,fontWeight: FontWeight.bold),),
            )
          ],
        )));
  }

  @override
  void initState() {
    super.initState();
    //开启倒计时
    countDown();
  }

  void countDown() {
    //设置倒计时三秒后执行跳转方法
    var duration = new Duration(seconds: 4);
    new Future.delayed(duration, goToHomePage);
  }

  void goToHomePage() async {
    //如果页面还未跳转过则跳转页面
    if (!isStartHomePage) {
      //跳转主页 且销毁当前页面
      UsersModel user = await UserDataCache.getInstance().getUser();
      if (user == null) {
        Navigator.of(context).pushAndRemoveUntil(
            new MaterialPageRoute(builder: (context) => new LoginPage('登录页')),
            (Route<dynamic> rout) => false);
      } else {
        await UserDataCache.getInstance().setUser(context, user); //保存用户数据
        Navigator.of(context).pushAndRemoveUntil(
            new MaterialPageRoute(builder: (context) => new HomePage('主页')),
            (Route<dynamic> rout) => false);
      }
      print(json.encode(user));
      isStartHomePage = true;
    }
  }
}
